package com.ace.user;

import java.util.List;

import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

public interface UserMapper {

	@Select("SELECT * FROM users LIMIT #{range} OFFSET #{start}")
	@Results(value = {
		@Result(property = "username", column = "username"),
		@Result(property = "password", column = "password"),
		@Result(property = "roles", javaType = List.class, column = "username", many = @Many(select = "getRoles")) 
	})
	public List<User> getAllUsers(@Param("start") int start, @Param("range") int range);

	@Select("SELECT authority FROM authorities WHERE username=#{username}")
	public List<String> getRoles(@Param("username") String username);

	@Select("SELECT count(username) FROM users")
	public int getUserCount();
	
	@Update("UPDATE users SET enabled=1 WHERE username=#{username}")
	public int enableUser(@Param("username") String username);
	
	@Update("UPDATE users SET enabled=0 WHERE username=#{username}")
	public int disableUser(@Param("username") String username);
	
	@Update("UPDATE users SET password=#{password} WHERE username=#{username}")
	public int changePassword(@Param("username") String username, @Param("password") String password);

}
